 function [logPDFNoConstant,logPDF]=matricvariateNPDF(x,muMat,Qprec,Pprec)
% 
%  matricvariateNPDF(x,muMat,Qprec,Pprec)
% 
% Computes the Log pdf of x~MN(muMat,Q kron P) 
% *Note* uses inv(P) and inv(Q) 
% Notation follows from Lubrano et. al page 301.
% The unormalized pdf will not be computed --hence no need to account for constant--
% if only one output argument is requested. 
% In omitting the constant it will ommit terms related to P not Q, since in
% the multivariante linear regression case Q will correspond to the VCV of
% the errors. 
%
% See for instance MNIWPDF.m 
%
%% Input 
% x:     [p,q] matrix
% muMat: [p,q] matrix with Mean 
% Qprec: [q,q] matrix with INV(Q) 
% Pprec: [p,p] matrix with INV(P)
% 
%% Output 
% logPDFNoConstant: log PDF without the normalizing constant 
% logPDF          : log PDF with normalizing constant 
% 
% Alejandro Justiniano August 30 2013 (C) 
% =========================================================================
[Nr,Nc]=size(x); 
logPDFNoConstant=-0.5*trace( Qprec*(x-muMat)'*Pprec*(x-muMat) )+0.5*Nr*log((det(Qprec))); 
% Check signs on the determinants are correct 
% fprintf('Max diff in det(Q) %1.10f \n', ...
%     abs( det(Qprec)^(Nr/2) - ( det( Qprec\eye(Nc) ) )^(-Nr/2)) ); 
% fprintf('Max diff in det(P) %1.10f \n', ...
%     abs( det(Pprec)^(Nc/2) - ( det( Pprec\eye(Nr) ) )^(-Nc/2)) ); 
if nargout < 2 
    return 
end 
logConstant=-0.5*Nr*Nc*log(2*pi)+0.5*Nc*log(det(Pprec)); 
logPDF=logPDFNoConstant+logConstant;